import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'

import {
  GET_RECOMMEND_DOCTORS,
  GET_DOCTOR_DETAIL,
  SYNC_DOCTOR_DETAIL,
  GET_DOCTOR_BY_TAG_IDS,
  GET_RECOMMEND_DOCTORS_HOME,
  GET_DOCTORS,
  SET_CURR_DCOTORDETAIL, GET_DOCTOR_BY_TAG_ID,
} from '@ACTIONS/doctor'
import { START_JOUNEY, START_SURVEY } from '@ACTIONS/decision'

const INITIAL_STATE = {
  recommendDoctorList: [],
  loadingRecommendDoctor: false,
  doctorDetail: {},
  loadingDoctorDetail: false,
  doctorListByTagIds: [],
  loadingDoctorListByTagIds: false,
  canLoadDoctorListByTagIds: false,
  recommendDoctorsHome: [],
  loadingRecommendDoctorsHome: false,
  servicePackDoctors: [],
  initServicePackDoctors: true,
  loadingServicePackDoctors: true,
  canLoadServicePackDoctors: true,
}
const rehydrate = (state, { payload }) => {
  if (payload && payload.doctor) {
    return { ...INITIAL_STATE, ...payload.doctor }
  }
  return { ...state }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,
  [GET_RECOMMEND_DOCTORS.REQUEST]: (state, { payload = {} }) => {
    const { skip } = payload
    return {
      ...state,
      loadingRecommendDoctor: skip === 0,
    }
  },
  [GET_RECOMMEND_DOCTORS.SUCCESS]: (state, { data: { Doctors }, skip }) => {
    const { recommendDoctorList } = state
    return {
      ...state,
      loadingRecommendDoctor: false,
      recommendDoctorList: skip === 0 ? Doctors : [...recommendDoctorList, ...Doctors],
    }
  },
  [GET_RECOMMEND_DOCTORS.FAILURE]: (state, { error }) => ({
    ...state,
    loadingRecommendDoctor: false,
    error,
  }),
  [GET_DOCTOR_DETAIL.REQUEST]: (state, { disabledLoading }) => {
    if (disabledLoading) return state
    return {
      ...state,
      doctorDetail: {},
      loadingDoctorDetail: true,
    }
  },
  [GET_DOCTOR_DETAIL.SUCCESS]: (state, { doctorDetail }) => ({
    ...state,
    doctorDetail,
    loadingDoctorDetail: false,
  }),
  [GET_DOCTOR_DETAIL.FAILURE]: (state, { error }) => ({
    ...state,
    error,
    loadingDoctorDetail: false,
  }),
  [SYNC_DOCTOR_DETAIL.REQUEST]: state => ({
    ...state,
    doctorDetail: {
      ...state.doctorDetail,
      Register: true,
      RegisterNow: false
    }
  }),

  [SET_CURR_DCOTORDETAIL.REQUEST]: (state, { payload }) => ({
    ...state,
    doctorDetail: payload
  }),

  [GET_DOCTOR_BY_TAG_IDS.REQUEST]: (state, { payload: { skip } }) => ({
    ...state,
    loadingDoctorListByTagIds: true,
    doctorListByTagIds: skip === 0 ? [] : state.doctorListByTagIds,
  }),
  [GET_DOCTOR_BY_TAG_IDS.SUCCESS]: (state, { doctorListByTagIds, canLoadMore }) => ({
    ...state,
    doctorListByTagIds: [...state.doctorListByTagIds, ...doctorListByTagIds],
    loadingDoctorListByTagIds: false,
    canLoadDoctorListByTagIds: canLoadMore
  }),
  [GET_DOCTOR_BY_TAG_IDS.FAILURE]: (state, { error }) => ({
    ...state,
    error,
    loadingDoctorListByTagIds: false
  }),

  [START_JOUNEY.REQUEST]: state => ({
    ...state,
    doctorListByTagIds: []
  }),
  [START_SURVEY.REQUEST]: state => ({
    ...state,
    doctorListByTagIds: []
  }),

  [GET_RECOMMEND_DOCTORS_HOME.SUCCESS]: (state, { data: { Doctors } }) => ({
    ...state,
    recommendDoctorsHome: Doctors,
    loadingRecommendDoctorsHome: false
  }),
  [GET_RECOMMEND_DOCTORS_HOME.REQUEST]: (state, { payload = {} }) => {
    const { skip } = payload
    return {
      ...state,
      loadingRecommendDoctorsHome: skip === 0,
    }
  },
  [GET_RECOMMEND_DOCTORS_HOME.FAILURE]: state => ({
    ...state,
    loadingRecommendDoctorsHome: false
  }),
  [GET_DOCTORS.REQUEST]: (state, { payload: { skip } }) => ({
    ...state,
    loadingServicePackDoctors: true,
    initServicePackDoctors: skip === 0 ? true : state.initServicePackDoctors,
    servicePackDoctors: skip === 0 ? [] : state.servicePackDoctors
  }),
  [GET_DOCTORS.SUCCESS]: (state, { data, canLoadMore, skip }) => ({
    ...state,
    servicePackDoctors: skip === 0 ? data : [...state.servicePackDoctors, ...data],
    canLoadServicePackDoctors: canLoadMore,
    loadingServicePackDoctors: false,
    initServicePackDoctors: false
  }),
  [GET_DOCTORS.FAILURE]: state => ({ ...state, loadingServicePackDoctors: false }),
  [GET_DOCTOR_BY_TAG_ID.REQUEST]: (state, { payload: { skip } }) => ({
    ...state,
    loadingDoctorListByTagIds: true,
    doctorListByTagIds: skip === 0 ? [] : state.doctorListByTagIds,
  }),
  [GET_DOCTOR_BY_TAG_ID.SUCCESS]: (state, { doctorListByTagIds, canLoadMore }) => ({
    ...state,
    doctorListByTagIds: [...state.doctorListByTagIds, ...doctorListByTagIds],
    loadingDoctorListByTagIds: false,
    canLoadDoctorListByTagIds: canLoadMore
  }),
  [GET_DOCTOR_BY_TAG_ID.FAILURE]: (state, { error }) => ({
    ...state,
    error,
    loadingDoctorListByTagIds: false
  }),
})
